package com.ymttest.database.mongo.jproduct;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.google.gson.JsonArray;
import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;
import com.ymt.database.MongoDBDriver;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.MapUtil;

public class ActivityProductsWapper {
	private MongoDBDriver mdd = YmtMongoDriverFactroy.getInstance_YmtProductDBdriverNewJava("ActivityProducts");
	
	public Map<String,Object> getByProdInActivityId(int prodInActivityId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"inaid\":" + prodInActivityId + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	public ArrayList<Map> getByProdInActivityIds(int prodInActivityId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"inaid\":" + prodInActivityId + "}",100);
	    
	    return list;
	}
	public List<Map> getAllByProdId(String prodId) throws Exception {
		List<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + prodId + "\"}",20);
	    return list;
	}
	
	
	public Map<String,Object> getByProdId(String prodId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + prodId + "\"}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	
	     
	public int getCountByProdInActivityId(int prodInActivityId) throws Exception{
		return mdd.selectPartLimitToMap("{\"inaid\":" + prodInActivityId + "}",50).size();
	}
	

	
	
	public boolean update(Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception{
		BasicDBObject queryObject = new BasicDBObject(searchMap);
		BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
		return  mdd.update(queryObject, updateObject,false,true).isUpdateOfExisting();
	}
	
	
	public boolean update(String productId,Map<String, Object> updateMap) throws Exception{
		return  mdd.update(MapUtil.hashMap("spid",productId), updateMap).isUpdateOfExisting();
	}
	
	
	public boolean update(int prodInActivityId,Map<String, Object> updateMap) throws Exception{
		return  mdd.update(MapUtil.hashMap("inaid",prodInActivityId), updateMap).isUpdateOfExisting();
	}
	/**
	 * 更新活动商品价格的开始时间
	 * @param inaid
	 * @param starttime
	 * @throws Exception
	 */
	public void updateStartTimeByInAid(Integer inaid,Date starttime) throws Exception{
		BasicDBObject queryObject=new BasicDBObject("inaid", inaid);
		BasicDBObject updateObject = new BasicDBObject("$set",  new BasicDBObject("start", starttime));
		WriteResult w = mdd.update(queryObject,updateObject,false,false);
		if(w.getN()==0){
			Thread.sleep(5000);
			w = mdd.update(queryObject,updateObject,false,false);
		}
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		/*List<BasicDBObject>  objects = new ArrayList<>();
		BasicDBObject doc = new BasicDBObject();
		doc.put("cid", "2922544a-aa95-43f0-96f7-ed4c8788ad24");
		doc.put("price", 321.00);
		doc.put("pric1e", 321.00);
		
		BasicDBObject doc1 = new BasicDBObject();
		doc1.put("cid", "f00e9510-0a60-410a-8f55-ec261bc0f5ec");
		doc1.put("price", 2.00);
		
		objects.add(doc);
		objects.add(doc1);
		
		
		System.out.println(new ActivityProductsWapper().update(MapUtil.hashMap("spid","940088f0-99f6-40f0-a594-c48c90864f28"),MapUtil.hashMap("catalogs", objects)));
	*/
		
		
		
		//System.out.println(new ActivityProductsWapper().update(MapUtil.hashMap("aid",2122966),MapUtil.hashMap("alimit", 13221)));
		System.out.println( new ActivityProductsWapper().getByProdId("6430b33e-f6d3-414f-ad7b-0312e4c4c450"));
		
		System.out.println(new JSONArray(new ActivityProductsWapper().getByProdId("6430b33e-f6d3-414f-ad7b-0312e4c4c450").get("catalogs").toString()).getJSONObject(0).get("stock"));
		
	//	System.out.println(new JSONObject( new ActivityProductsWapper().getByProdId("6430b33e-f6d3-414f-ad7b-0312e4c4c450")).getJSONObject("catalogs").get("stock"));
		
		
		

	}
	
	

}
